package com.woniu108.evaluate.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.evaluate.dto.ProductReviewDto;
import com.woniu108.evaluate.form.ProductReviewPublishForm;
import com.woniu108.evaluate.form.ProductReviewReplyForm;
import com.woniu108.evaluate.model.ProductReview;
import com.woniu108.evaluate.param.ProductReviewPublishParam;
import com.woniu108.evaluate.param.ProductReviewReplyParam;
import com.woniu108.evaluate.service.ProductReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品评价表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/productReview")
@Api(tags = "商品评价接口")
public class ProductReviewController {
    @Resource
    private ProductReviewService productReviewService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * token获取用户id
     *
     * @param token 会话跟踪
     * @return 用户id
     */
    public String getUserIdByToken(String token) {
        String refreshToken = redisUtil.getString(token);
        return JWTUtil.parseUserToken(refreshToken).get("userId").toString();
    }

    /**
     * token中获取管理员id
     *
     * @param token
     * @return
     */
    public String getAdminId(String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        return adminId;
    }

    /**
     * 查询某个商品下所有评价
     *
     * @param pItemId 商品id
     * @return Dto集合 商品所有评价
     */
    @GetMapping("/queryProductReviewByPItemId/{pItemId}.c")
    @ApiOperation("查询某个商品下所有评价")
    public Result queryProductReviewByPItemId(@PathVariable("pItemId") @ApiParam("商品id") Integer pItemId) {
        List<ProductReviewDto> productReviewDtos = productReviewService.selectProductReviewByPItemId(pItemId);
        return Result.newListInstance(ProductReviewDto.class).setData(productReviewDtos);
    }

    /**
     * 发布评价(用户)
     *
     * @param form  评价正文以及订单编号
     * @param token userId
     * @return Boolean值
     */
    @PostMapping("/publishReviews")
    @ApiOperation("发布评价(用户)")
    public Result<Boolean> publishReviews(@RequestBody ProductReviewPublishForm form, @RequestHeader String token) {
        String spokeId = getUserIdByToken(token);

        ProductReviewPublishParam param = BeanUtil.toBean(form, ProductReviewPublishParam.class);
        param.setSpokeId(spokeId);
        Boolean aBoolean = productReviewService.publishReviewsByUser(param);
        return Result.getResult(Boolean.class).setMsg("发布评价成功").setData(aBoolean);
    }

    /**
     * 管理员回复评论
     *
     * @param form  评价表id，回复正文
     * @param token
     * @return 真假值
     */
    @PostMapping("/replyByReview.api")
    @ApiOperation("管理员回复评论")
    @AdminHistoryLog
    public Result<Boolean> replyByReview(@RequestBody ProductReviewReplyForm form, @RequestHeader String token) {
        String adminId = getAdminId(token);

        ProductReviewReplyParam param = BeanUtil.toBean(form, ProductReviewReplyParam.class);
        param.setAdminId(adminId);
        Boolean aBoolean = productReviewService.replyByReview(param);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("回复成功");
    }

    /**
     * 删除用户自己的评论
     *
     * @param proReviewId
     * @param token
     * @return
     */
    @GetMapping("/delReviewByIdToUser/{proReviewId}")
    @ApiOperation("删除用户自己的评论")
    public Result<Boolean> delReviewByIdToUser(@PathVariable("proReviewId") @ApiParam("评论id") Integer proReviewId, @RequestHeader String token) {
        String userId = getUserIdByToken(token);

        Boolean aBoolean = productReviewService.delReviewByIdToUser(proReviewId, userId);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("删除成功");
    }

    /**
     * 删除评论(管理员)
     *
     * @param proReviewId
     * @return
     */
    @GetMapping("/delReviewById/{proReviewId}.api")
    @ApiOperation("删除评论(管理员)")
    @AdminHistoryLog
    public Result<Boolean> delReviewById(@PathVariable("proReviewId") @ApiParam("评论id") Integer proReviewId) {

        Boolean aBoolean = productReviewService.delReviewById(proReviewId);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("删除成功");
    }
}

